(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.ratingType.createRejectController', createRejectController);
	createRejectController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'ratingTypeService' ];
	function createRejectController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, ratingTypeService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.ratingType = {};
		ctrl.ratingType.ratingGrades = ctrl.ratingType.ratingGrades || [];
		ctrl.ratingType.ratingQaItems = ctrl.ratingType.ratingQaItems || [];
		ctrl.ratingType.ratingQaAdjs = ctrl.ratingType.ratingQaAdjs || [];
		
		ctrl.init = function() {
			ratingTypeService.initData($scope, ctrl);
			authService.createReject(params).then(function(response) {
				ctrl.ratingType = response.data.model || params;
				ctrl.ratingType.taskId = response.data.task.taskId;
				ctrl.ratingType.ratingGrades = ctrl.ratingType.ratingGrades || [];
				ctrl.ratingType.ratingQaItems = ctrl.ratingType.ratingQaItems || [];
				ctrl.ratingType.ratingQaAdjs = ctrl.ratingType.ratingQaAdjs || [];
			});
		};
		ctrl.createRejectSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else {
				return authService.createRejectSave(ctrl.ratingType).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createRejectSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else {
				return authService.createRejectSubmit(ctrl.ratingType).then(submitSuccessful, submitFailed);
			}
		};

		ctrl.createRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.ratingType.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createRejectCancel(ctrl.ratingType).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};



		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.ratingtype.list',
					'state' : 'cda.ratingType.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.merge(ctrl.ratingType, response.data.model);
			} else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.ratingtype.list',
					'state' : 'cda.ratingType.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);